package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.bean.goods.Places;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.order.service.IPlacesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service.impl
 * @Author ZhongYao_H
 * @Date 2019/5/23:13:56
 * @Year 2019 Month 05 Day 23
 * @Date_zh 2019 年 五月 星期四
 */
@Service
public class PlacesServiceImpl extends BaseServiceImpl<Places,String> implements IPlacesService {

    @Autowired
    IBaseRepository<User,String> userStringIBaseRepository;

    @Override
    public Places getByOrgId(String adminId,String id,String type) {
        List<Places> list = query("from Places where orgId = ? and type = ? ", new Object[]{adminId,type});
        Places places = list.size() > 0 ? list.get(0) : null;
        List<Map<String, Object>> maps = userStringIBaseRepository.findListBySql("SELECT sum(p.num) as sum from places p where p.orgId in (SELECT id from common_user where orgId = ?) and p.type = ?  ", new Object[]{id, type});
        for (Map<String, Object> map : maps) {
            if (places!=null){
                places.setNum(map.get("sum").toString());
            }
        }
        return places;
    }

    @Override
    public Places getByOrgId(String id, String type) {
        List<Places> list = query("from Places where orgId = ? and type = ? ", new Object[]{id,type});
        return list.size()>0?list.get(0):null;
    }

    @Override
    public Places getByAdminId(String id, String type) {
            List<Places> list = query("from Places where orgId = ? and type = ? ", new Object[]{id,type});
            return list.size()>0?list.get(0):null;
    }
}
